using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class ContractDetailDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procContractDetail_add]";
        private string _strSPUpdateName = "dbo.[procContractDetail_update]";
        private string _strSPDeleteName = "dbo.[procContractDetail_delete]";
        private string _strSPGetName = "dbo.[procContractDetail_get]";
        private string _strSPGetAllName = "dbo.[procContractDetail_getall]";
        private string _strSPGetPages = "dbo.[procContractDetail_getpaged]";
        private string _strSPIsExist = "dbo.[procContractDetail_isexist]";
        private string _strTableName = "ContractDetail";

        private string _strSPInsertName1 = "dbo.[procContractDetail1_add]";
        private string _strSPUpdateName1 = "dbo.[procContractDetail1_update]";
        private string _strSPDeleteName1 = "dbo.[procContractDetail1_delete]";
        private string _strSPGetName1 = "dbo.[procContractDetail1_get]";
        private string _strSPGetAllName1 = "dbo.[procContractDetail1_getall]";
        private string _strSPGetPages1 = "dbo.[procContractDetail1_getpaged]";
        private string _strSPIsExist1 = "dbo.[procContractDetail1_isexist]";



        #endregion Local Variable

        #region Method
        public ContractDetailInfo Get_ContractDetail(
            Int32 ID
        , ref string sErr)
        {
            ContractDetailInfo objEntr = new ContractDetailInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter("ID", ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (ContractDetailInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public ContractDetailInfo Get_ContractDetail1(
           Int32 ID
       , ref string sErr)
        {
            ContractDetailInfo objEntr = new ContractDetailInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter("ID", ID);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (ContractDetailInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ContractDetailInfo result = new ContractDetailInfo();
            result.ID = (dt.Rows[i]["ID"] == DBNull.Value ? -1 : (Int32)dt.Rows[i]["ID"]);
            result.Contract = (dt.Rows[i]["Contract"] == DBNull.Value ? "" : (String)dt.Rows[i]["Contract"]);
            result.LimitAmt = (dt.Rows[i]["LimitAmt"] == DBNull.Value ? 0 : (Double)dt.Rows[i]["LimitAmt"]);
            result.LimitAmtOther = (dt.Rows[i]["LimitAmtOther"] == DBNull.Value ? 0 : (Double)dt.Rows[i]["LimitAmtOther"]);
            result.CIT = (dt.Rows[i]["CIT"] == DBNull.Value ? 0 : (Double)dt.Rows[i]["CIT"]);
            result.VAT = (dt.Rows[i]["VAT"] == DBNull.Value ? 0 : (Double)dt.Rows[i]["VAT"]);
            result.Addendum = (dt.Rows[i]["Addendum"] == DBNull.Value ? "" : (String)dt.Rows[i]["Addendum"]);
            result.SignDate = (dt.Rows[i]["SignDate"] == DBNull.Value ? DateTime.Now : (DateTime)dt.Rows[i]["SignDate"]);
            result.EffectiveDate = (dt.Rows[i]["EffectiveDate"] == DBNull.Value ? DateTime.Now : (DateTime)dt.Rows[i]["EffectiveDate"]);
            result.ExpiryDate = (dt.Rows[i]["ExpiryDate"] == DBNull.Value ? DateTime.Now : (DateTime)dt.Rows[i]["ExpiryDate"]);
            result.MCApproval = (dt.Rows[i]["MCApproval"] == DBNull.Value ? -1 : (Int32)dt.Rows[i]["MCApproval"]);
            result.ActionRequired = (dt.Rows[i]["ActionRequired"] == DBNull.Value ? "" : (String)dt.Rows[i]["ActionRequired"]);
            result.Status = (dt.Rows[i]["Status"] == DBNull.Value ? "" : (String)dt.Rows[i]["Status"]);
            result.Remark1 = (dt.Rows[i]["Remark1"] == DBNull.Value ? "" : (String)dt.Rows[i]["Remark1"]);
            result.Remark2 = (dt.Rows[i]["Remark2"] == DBNull.Value ? "" : (String)dt.Rows[i]["Remark2"]);
            result.Remark3 = (dt.Rows[i]["Remark3"] == DBNull.Value ? "" : (String)dt.Rows[i]["Remark3"]);
            result.BudgetCode = (dt.Rows[i]["BudgetCode"] == DBNull.Value ? "" : (String)dt.Rows[i]["BudgetCode"]);
            result.BudgetYear = (dt.Rows[i]["BudgetYear"] == DBNull.Value ? -1 : (Int32)dt.Rows[i]["BudgetYear"]);
            result.OrderReq = (dt.Rows[i]["OrderReq"] == DBNull.Value ? "" : (String)dt.Rows[i]["OrderReq"]);
            result.YearBudgetCode = (dt.Rows[i]["YearBudgetCode"] == DBNull.Value ? "" : (String)dt.Rows[i]["YearBudgetCode"]);
            result.aType = (dt.Rows[i]["aType"] == DBNull.Value ? "" : (String)dt.Rows[i]["aType"]);
            result.Period = (dt.Rows[i]["Period"] == DBNull.Value ? -1 : (Int32)dt.Rows[i]["Period"]);

            return result;
        }

        public DataTable GetAll_ContractDetail(ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        public DataTable GetAll_ContractDetail1(ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName1);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add_ContractDetail(ContractDetailInfo objEntr, ref string sErr)
        {

            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);

            AddParameter("ID", objEntr.ID);
            AddParameter("Contract", objEntr.Contract);
            AddParameter("LimitAmt", objEntr.LimitAmt);
            //AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            if (objEntr.LimitAmtOther == 0)
                AddParameter("LimitAmtOther", DBNull.Value);
            else
                AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            //AddParameter("CIT", objEntr.CIT);
            if (objEntr.CIT == 0)
                AddParameter("CIT", DBNull.Value);
            else
                AddParameter("CIT", objEntr.CIT);

            //AddParameter("VAT", objEntr.VAT);
            if (objEntr.VAT == 0)
                AddParameter("VAT", DBNull.Value);
            else
                AddParameter("VAT", objEntr.VAT);

            //AddParameter("Addendum", objEntr.Addendum);
            if (objEntr.Addendum == "")
                AddParameter("Addendum", DBNull.Value);
            else
                AddParameter("Addendum", objEntr.Addendum);

            //AddParameter("SignDate", objEntr.SignDate);
            if (objEntr.SignDate == DateTime.MinValue)
                AddParameter("SignDate", DBNull.Value);
            else
                AddParameter("SignDate", objEntr.SignDate);

            //AddParameter("EffectiveDate", objEntr.EffectiveDate);
            if (objEntr.EffectiveDate == DateTime.MinValue)
                AddParameter("EffectiveDate", DBNull.Value);
            else
                AddParameter("EffectiveDate", objEntr.EffectiveDate);

            //AddParameter("ExpiryDate", objEntr.ExpiryDate);
            if (objEntr.ExpiryDate == DateTime.MinValue)
                AddParameter("ExpiryDate", DBNull.Value);
            else
                AddParameter("ExpiryDate", objEntr.ExpiryDate);

            //AddParameter("MCApproval", objEntr.MCApproval);
            if (objEntr.MCApproval == -1)
                AddParameter("MCApproval", DBNull.Value);
            else
                AddParameter("MCApproval", objEntr.MCApproval);

            //AddParameter("ActionRequired", objEntr.ActionRequired);
            if (objEntr.ActionRequired == "")
                AddParameter("ActionRequired", DBNull.Value);
            else
                AddParameter("ActionRequired", objEntr.ActionRequired);

            //AddParameter("Status", objEntr.Status);

            if (objEntr.Status == "")
                AddParameter("Status", DBNull.Value);
            else
                AddParameter("Status", objEntr.Status);

            //AddParameter("Remark1", objEntr.Remark1);
            if (objEntr.Remark1 == "")
                AddParameter("Remark1", DBNull.Value);
            else
                AddParameter("Remark1", objEntr.Remark1);

            //AddParameter("Remark2", objEntr.Remark2);
            if (objEntr.Remark2 == "")
                AddParameter("Remark2", DBNull.Value);
            else
                AddParameter("Remark2", objEntr.Remark2);

            //AddParameter("Remark3", objEntr.Remark3);
            if (objEntr.Remark3 == "")
                AddParameter("Remark3", DBNull.Value);
            else
                AddParameter("Remark3", objEntr.Remark3);

            //AddParameter("BudgetCode", objEntr.BudgetCode);
            if (objEntr.BudgetCode == "")
                AddParameter("BudgetCode", DBNull.Value);
            else
                AddParameter("BudgetCode", objEntr.BudgetCode);

            //AddParameter("BudgetYear", objEntr.BudgetYear);
            if (objEntr.BudgetYear == 0)
                AddParameter("BudgetYear", DBNull.Value);
            else
                AddParameter("BudgetYear", objEntr.BudgetYear);

            //AddParameter("OrderReq", objEntr.OrderReq);
            if (objEntr.OrderReq == "")
                AddParameter("OrderReq", DBNull.Value);
            else
                AddParameter("OrderReq", objEntr.OrderReq);

            //AddParameter("YearBudgetCode", objEntr.YearBudgetCode);
            if (objEntr.YearBudgetCode == "")
                AddParameter("YearBudgetCode", DBNull.Value);
            else
                AddParameter("YearBudgetCode", objEntr.YearBudgetCode);

            //AddParameter("aType", objEntr.aType);
            if (objEntr.aType == "")
                AddParameter("aType", DBNull.Value);
            else
                AddParameter("aType", objEntr.aType);

            //AddParameter("Period", objEntr.Period);
            if (objEntr.Period == 0)
                AddParameter("Period", DBNull.Value);
            else
                AddParameter("Period", objEntr.Period);


            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public Int32 Add_ContractDetail1(ContractDetailInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName1);
            AddParameter("ID", objEntr.ID);
            AddParameter("Contract", objEntr.Contract);
            AddParameter("LimitAmt", objEntr.LimitAmt);
            //AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            if (objEntr.LimitAmtOther == 0)
                AddParameter("LimitAmtOther", DBNull.Value);
            else
                AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            //AddParameter("CIT", objEntr.CIT);
            if (objEntr.CIT == 0)
                AddParameter("CIT", DBNull.Value);
            else
                AddParameter("CIT", objEntr.CIT);

            //AddParameter("VAT", objEntr.VAT);
            if (objEntr.VAT == 0)
                AddParameter("VAT", DBNull.Value);
            else
                AddParameter("VAT", objEntr.VAT);

            //AddParameter("Addendum", objEntr.Addendum);
            if (objEntr.Addendum == "")
                AddParameter("Addendum", DBNull.Value);
            else
                AddParameter("Addendum", objEntr.Addendum);

            //AddParameter("SignDate", objEntr.SignDate);
            if (objEntr.SignDate == DateTime.MinValue)
                AddParameter("SignDate", DBNull.Value);
            else
                AddParameter("SignDate", objEntr.SignDate);

            //AddParameter("EffectiveDate", objEntr.EffectiveDate);
            if (objEntr.EffectiveDate == DateTime.MinValue)
                AddParameter("EffectiveDate", DBNull.Value);
            else
                AddParameter("EffectiveDate", objEntr.EffectiveDate);

            //AddParameter("ExpiryDate", objEntr.ExpiryDate);
            if (objEntr.ExpiryDate == DateTime.MinValue)
                AddParameter("ExpiryDate", DBNull.Value);
            else
                AddParameter("ExpiryDate", objEntr.ExpiryDate);

            //AddParameter("MCApproval", objEntr.MCApproval);
            if (objEntr.MCApproval == -1)
                AddParameter("MCApproval", DBNull.Value);
            else
                AddParameter("MCApproval", objEntr.MCApproval);

            //AddParameter("ActionRequired", objEntr.ActionRequired);
            if (objEntr.ActionRequired == "")
                AddParameter("ActionRequired", DBNull.Value);
            else
                AddParameter("ActionRequired", objEntr.ActionRequired);

            //AddParameter("Status", objEntr.Status);

            if (objEntr.Status == "")
                AddParameter("Status", DBNull.Value);
            else
                AddParameter("Status", objEntr.Status);

            //AddParameter("Remark1", objEntr.Remark1);
            if (objEntr.Remark1 == "")
                AddParameter("Remark1", DBNull.Value);
            else
                AddParameter("Remark1", objEntr.Remark1);

            //AddParameter("Remark2", objEntr.Remark2);
            if (objEntr.Remark2 == "")
                AddParameter("Remark2", DBNull.Value);
            else
                AddParameter("Remark2", objEntr.Remark2);

            //AddParameter("Remark3", objEntr.Remark3);
            if (objEntr.Remark3 == "")
                AddParameter("Remark3", DBNull.Value);
            else
                AddParameter("Remark3", objEntr.Remark3);

            //AddParameter("BudgetCode", objEntr.BudgetCode);
            if (objEntr.BudgetCode == "")
                AddParameter("BudgetCode", DBNull.Value);
            else
                AddParameter("BudgetCode", objEntr.BudgetCode);

            //AddParameter("BudgetYear", objEntr.BudgetYear);
            if (objEntr.BudgetYear == 0)
                AddParameter("BudgetYear", DBNull.Value);
            else
                AddParameter("BudgetYear", objEntr.BudgetYear);

            //AddParameter("OrderReq", objEntr.OrderReq);
            if (objEntr.OrderReq == "")
                AddParameter("OrderReq", DBNull.Value);
            else
                AddParameter("OrderReq", objEntr.OrderReq);

            //AddParameter("YearBudgetCode", objEntr.YearBudgetCode);
            if (objEntr.YearBudgetCode == "")
                AddParameter("YearBudgetCode", DBNull.Value);
            else
                AddParameter("YearBudgetCode", objEntr.YearBudgetCode);

            //AddParameter("aType", objEntr.aType);
            if (objEntr.aType == "")
                AddParameter("aType", DBNull.Value);
            else
                AddParameter("aType", objEntr.aType);

            //AddParameter("Period", objEntr.Period);
            if (objEntr.Period == 0)
                AddParameter("Period", DBNull.Value);
            else
                AddParameter("Period", objEntr.Period);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update_ContractDetail(ContractDetailInfo objEntr)
        {

            openConnection();
            InitSPCommand(_strSPUpdateName);

            AddParameter("ID", objEntr.ID);
            AddParameter("Contract", objEntr.Contract);
            AddParameter("LimitAmt", objEntr.LimitAmt);
            //AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            if (objEntr.LimitAmtOther == 0)
                AddParameter("LimitAmtOther", DBNull.Value);
            else
                AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            //AddParameter("CIT", objEntr.CIT);
            if (objEntr.CIT == 0)
                AddParameter("CIT", DBNull.Value);
            else
                AddParameter("CIT", objEntr.CIT);

            //AddParameter("VAT", objEntr.VAT);
            if (objEntr.VAT == 0)
                AddParameter("VAT", DBNull.Value);
            else
                AddParameter("VAT", objEntr.VAT);

            //AddParameter("Addendum", objEntr.Addendum);
            if (objEntr.Addendum == "")
                AddParameter("Addendum", DBNull.Value);
            else
                AddParameter("Addendum", objEntr.Addendum);

            //AddParameter("SignDate", objEntr.SignDate);
            if (objEntr.SignDate == DateTime.MinValue)
                AddParameter("SignDate", DBNull.Value);
            else
                AddParameter("SignDate", objEntr.SignDate);

            //AddParameter("EffectiveDate", objEntr.EffectiveDate);
            if (objEntr.EffectiveDate == DateTime.MinValue)
                AddParameter("EffectiveDate", DBNull.Value);
            else
                AddParameter("EffectiveDate", objEntr.EffectiveDate);

            //AddParameter("ExpiryDate", objEntr.ExpiryDate);
            if (objEntr.ExpiryDate == DateTime.MinValue)
                AddParameter("ExpiryDate", DBNull.Value);
            else
                AddParameter("ExpiryDate", objEntr.ExpiryDate);

            //AddParameter("MCApproval", objEntr.MCApproval);
            if (objEntr.MCApproval == -1)
                AddParameter("MCApproval", DBNull.Value);
            else
                AddParameter("MCApproval", objEntr.MCApproval);

            //AddParameter("ActionRequired", objEntr.ActionRequired);
            if (objEntr.ActionRequired == "")
                AddParameter("ActionRequired", DBNull.Value);
            else
                AddParameter("ActionRequired", objEntr.ActionRequired);

            //AddParameter("Status", objEntr.Status);

            if (objEntr.Status == "")
                AddParameter("Status", DBNull.Value);
            else
                AddParameter("Status", objEntr.Status);

            //AddParameter("Remark1", objEntr.Remark1);
            if (objEntr.Remark1 == "")
                AddParameter("Remark1", DBNull.Value);
            else
                AddParameter("Remark1", objEntr.Remark1);

            //AddParameter("Remark2", objEntr.Remark2);
            if (objEntr.Remark2 == "")
                AddParameter("Remark2", DBNull.Value);
            else
                AddParameter("Remark2", objEntr.Remark2);

            //AddParameter("Remark3", objEntr.Remark3);
            if (objEntr.Remark3 == "")
                AddParameter("Remark3", DBNull.Value);
            else
                AddParameter("Remark3", objEntr.Remark3);

            //AddParameter("BudgetCode", objEntr.BudgetCode);
            if (objEntr.BudgetCode == "")
                AddParameter("BudgetCode", DBNull.Value);
            else
                AddParameter("BudgetCode", objEntr.BudgetCode);

            //AddParameter("BudgetYear", objEntr.BudgetYear);
            if (objEntr.BudgetYear == 0)
                AddParameter("BudgetYear", DBNull.Value);
            else
                AddParameter("BudgetYear", objEntr.BudgetYear);

            //AddParameter("OrderReq", objEntr.OrderReq);
            if (objEntr.OrderReq == "")
                AddParameter("OrderReq", DBNull.Value);
            else
                AddParameter("OrderReq", objEntr.OrderReq);

            //AddParameter("YearBudgetCode", objEntr.YearBudgetCode);
            if (objEntr.YearBudgetCode == "")
                AddParameter("YearBudgetCode", DBNull.Value);
            else
                AddParameter("YearBudgetCode", objEntr.YearBudgetCode);

            //AddParameter("aType", objEntr.aType);
            if (objEntr.aType == "")
                AddParameter("aType", DBNull.Value);
            else
                AddParameter("aType", objEntr.aType);

            //AddParameter("Period", objEntr.Period);
            if (objEntr.Period == 0)
                AddParameter("Period", DBNull.Value);
            else
                AddParameter("Period", objEntr.Period);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Update_ContractDetail1(ContractDetailInfo objEntr)
        {

            openConnection();
            InitSPCommand(_strSPUpdateName1);

            AddParameter("ID", objEntr.ID);
            AddParameter("Contract", objEntr.Contract);
            AddParameter("LimitAmt", objEntr.LimitAmt);
            //AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            if (objEntr.LimitAmtOther == 0)
                AddParameter("LimitAmtOther", DBNull.Value);
            else
                AddParameter("LimitAmtOther", objEntr.LimitAmtOther);
            //AddParameter("CIT", objEntr.CIT);
            if (objEntr.CIT == 0)
                AddParameter("CIT", DBNull.Value);
            else
                AddParameter("CIT", objEntr.CIT);

            //AddParameter("VAT", objEntr.VAT);
            if (objEntr.VAT == 0)
                AddParameter("VAT", DBNull.Value);
            else
                AddParameter("VAT", objEntr.VAT);

            //AddParameter("Addendum", objEntr.Addendum);
            if (objEntr.Addendum == "")
                AddParameter("Addendum", DBNull.Value);
            else
                AddParameter("Addendum", objEntr.Addendum);

            //AddParameter("SignDate", objEntr.SignDate);
            if (objEntr.SignDate == DateTime.MinValue)
                AddParameter("SignDate", DBNull.Value);
            else
                AddParameter("SignDate", objEntr.SignDate);

            //AddParameter("EffectiveDate", objEntr.EffectiveDate);
            if (objEntr.EffectiveDate == DateTime.MinValue)
                AddParameter("EffectiveDate", DBNull.Value);
            else
                AddParameter("EffectiveDate", objEntr.EffectiveDate);

            //AddParameter("ExpiryDate", objEntr.ExpiryDate);
            if (objEntr.ExpiryDate == DateTime.MinValue)
                AddParameter("ExpiryDate", DBNull.Value);
            else
                AddParameter("ExpiryDate", objEntr.ExpiryDate);

            //AddParameter("MCApproval", objEntr.MCApproval);
            if (objEntr.MCApproval == -1)
                AddParameter("MCApproval", DBNull.Value);
            else
                AddParameter("MCApproval", objEntr.MCApproval);

            //AddParameter("ActionRequired", objEntr.ActionRequired);
            if (objEntr.ActionRequired == "")
                AddParameter("ActionRequired", DBNull.Value);
            else
                AddParameter("ActionRequired", objEntr.ActionRequired);

            //AddParameter("Status", objEntr.Status);

            if (objEntr.Status == "")
                AddParameter("Status", DBNull.Value);
            else
                AddParameter("Status", objEntr.Status);

            //AddParameter("Remark1", objEntr.Remark1);
            if (objEntr.Remark1 == "")
                AddParameter("Remark1", DBNull.Value);
            else
                AddParameter("Remark1", objEntr.Remark1);

            //AddParameter("Remark2", objEntr.Remark2);
            if (objEntr.Remark2 == "")
                AddParameter("Remark2", DBNull.Value);
            else
                AddParameter("Remark2", objEntr.Remark2);

            //AddParameter("Remark3", objEntr.Remark3);
            if (objEntr.Remark3 == "")
                AddParameter("Remark3", DBNull.Value);
            else
                AddParameter("Remark3", objEntr.Remark3);

            //AddParameter("BudgetCode", objEntr.BudgetCode);
            if (objEntr.BudgetCode == "")
                AddParameter("BudgetCode", DBNull.Value);
            else
                AddParameter("BudgetCode", objEntr.BudgetCode);

            //AddParameter("BudgetYear", objEntr.BudgetYear);
            if (objEntr.BudgetYear == 0)
                AddParameter("BudgetYear", DBNull.Value);
            else
                AddParameter("BudgetYear", objEntr.BudgetYear);

            //AddParameter("OrderReq", objEntr.OrderReq);
            if (objEntr.OrderReq == "")
                AddParameter("OrderReq", DBNull.Value);
            else
                AddParameter("OrderReq", objEntr.OrderReq);

            //AddParameter("YearBudgetCode", objEntr.YearBudgetCode);
            if (objEntr.YearBudgetCode == "")
                AddParameter("YearBudgetCode", DBNull.Value);
            else
                AddParameter("YearBudgetCode", objEntr.YearBudgetCode);

            //AddParameter("aType", objEntr.aType);
            if (objEntr.aType == "")
                AddParameter("aType", DBNull.Value);
            else
                AddParameter("aType", objEntr.aType);

            //AddParameter("Period", objEntr.Period);
            if (objEntr.Period == 0)
                AddParameter("Period", DBNull.Value);
            else
                AddParameter("Period", objEntr.Period);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete_ContractDetail(
            Int32 ID
        )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter("ID", ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete_ContractDetail1(
            Int32 ID
        )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName1);
            AddParameter("ID", ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

       

        public Boolean IsExist_ContractDetail(
            Int32 ID
        )
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);

            AddParameter("ID", ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        public Boolean IsExist_ContractDetail1(
           Int32 ID
       )
        {
            InitConnect();
            InitSPCommand(_strSPIsExist1);

            AddParameter("ID", ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(ContractDetailInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method


        public string DeleteHL(string contract)
        {
            openConnection();
            InitCommand(@"DELETE FROM ContractDetail
                WHERE Contract = @Contract", CommandType.Text);
            AddParameter("Contract", contract);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string DeleteHV(string contract)
        {
            openConnection();
            InitCommand(@"DELETE FROM ContractDetail1
                WHERE Contract = @Contract", CommandType.Text);
            AddParameter("Contract", contract);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public DataTable GetAllHV(string contract, ref string sErr)
        {
            InitConnect();
            InitCommand(@"SELECT ID, Contract, LimitAmt, LimitAmtOther, CIT, VAT, Addendum, SignDate, EffectiveDate, ExpiryDate, MCApproval, ActionRequired, Status, Remark1, Remark2, Remark3, 
                      BudgetCode, BudgetYear, OrderReq, YearBudgetCode, aType, Period
                FROM ContractDetail1
                WHERE Contract = @Contract
                ORDER BY ID DESC", CommandType.Text);
            AddParameter("Contract", contract);

            DataTable dt = new DataTable();
            try
            {
                dt = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dt;
        }

        public DataTable GetAllHL(string contract, ref string sErr)
        {
            InitConnect();
            InitCommand(@"SELECT ID, Contract, LimitAmt, LimitAmtOther, CIT, VAT, Addendum, SignDate, EffectiveDate, ExpiryDate, MCApproval, ActionRequired, Status, Remark1, Remark2, Remark3, 
                      BudgetCode, BudgetYear, OrderReq, YearBudgetCode, aType, Period
                FROM ContractDetail
                WHERE Contract = @Contract
                ORDER BY ID DESC", CommandType.Text);
            AddParameter("Contract", contract);

            DataTable dt = new DataTable();
            try
            {
                dt = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            if (sErr != "") ErrorLog.SetLog(sErr);
            return dt;
        }
    }
}
